package com.zhong.beautifulimageapp.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhong.beautifulimageapp.R;
import com.zhong.beautifulimageapp.model.EventBusMessageAboutUserName;
import com.zhong.beautifulimageapp.model.TopicBriefItem;
import com.zhong.beautifulimageapp.utils.DateTimeDistance;
import com.zhong.beautifulimageapp.utils.DensityUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * RecyclerView的适配器
 * Created by 俊峰 on 2017/7/11.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {

    private List<TopicBriefItem> mBriefItems;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private int flag;
    private int marginLeft;

    public RecyclerViewAdapter(List<TopicBriefItem> briefItems, Context context, int flag) {
        mContext = context;
        mBriefItems = briefItems;
        mLayoutInflater = LayoutInflater.from(context);
        this.flag = flag;
        marginLeft = DensityUtil.dip2px(mContext, 5);
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.activity_topic_item, parent, false);
        return new RecyclerViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        final TopicBriefItem item = mBriefItems.get(position);
        holder.mItemTopicAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String topicId = item.getId();
                EventBus.getDefault().post(topicId);
            }
        });
        holder.mItemTopicTitle.setText(item.getTitle());
        if (item.isTop()) {
            if (flag != 0) {
                holder.mItemTopicGoodOrTop.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.mItemTopicTitle.getLayoutParams();
                params.setMargins(marginLeft, 0, 0, 0);
                holder.mItemTopicTitle.setLayoutParams(params);
            }
            holder.mItemTopicGoodOrTop.setBackgroundColor(0xFF1be617);
            holder.mItemTopicGoodOrTop.setTextColor(0xFFFFFFFF);
            holder.mItemTopicGoodOrTop.setText("置顶");
        } else if (!item.isTop() && item.isGood()) {
            if (flag != 0) {
                holder.mItemTopicGoodOrTop.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.mItemTopicTitle.getLayoutParams();
                params.setMargins(marginLeft, 0, 0, 0);
                holder.mItemTopicTitle.setLayoutParams(params);
            }
            holder.mItemTopicGoodOrTop.setBackgroundColor(0xFF1be617);
            holder.mItemTopicGoodOrTop.setTextColor(0xFFFFFFFF);
            holder.mItemTopicGoodOrTop.setText("精华");
        } else if (!item.isTop() && !item.isGood()) {
            if (flag == 0) {
                holder.mItemTopicGoodOrTop.setBackgroundColor(0xFFE0E0E0);
                holder.mItemTopicGoodOrTop.setTextColor(0xFF000000);
                String text = null;
                if ("share".equals(item.getTab())) {
                    text = "分享";
                } else if ("ask".equals(item.getTab())) {
                    text = "问答";
                } else if ("job".equals(item.getTab())) {
                    text = "招聘";
                } else if (item.getTab() == null){
                    text = "普通";
                }
                holder.mItemTopicGoodOrTop.setText(text);
            } else {
                holder.mItemTopicGoodOrTop.setVisibility(View.GONE);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.mItemTopicTitle.getLayoutParams();
                params.setMargins(0, 0, 0, 0);
                holder.mItemTopicTitle.setLayoutParams(params);
            }
        }
        holder.mItemTopicUserName.setText(item.getAuthor().getLoginname());
        holder.mItemTopicReplayCount.setText(String.valueOf(item.getReply_count()));
        holder.mItemTopicSeeCount.setText(String.valueOf(item.getVisit_count()));
        holder.mItemTopicReplayTime.setText(DateTimeDistance.getTimeDistance(item.getLast_reply_at(), 1));
        holder.mItemTopicTime.setText(DateTimeDistance.getTimeDistance(item.getCreate_at(), 0));
        Glide.with(mContext).load(item.getAuthor()
                .getAvatar_url())
                .asBitmap()
                .placeholder(R.mipmap.app)
                .into(holder.mItemTopicUserIcon);
        holder.mItemTopicUserIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBusMessageAboutUserName name = new EventBusMessageAboutUserName();
                name.setLoginName(item.getAuthor().getLoginname());
                EventBus.getDefault().post(name);
            }
        });
    }

    @Override
    public int getItemCount() {
        int count = 0;
        if (mBriefItems != null) {
            count = mBriefItems.size();
        }
        return count;
    }

    static class RecyclerViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_topic_user_name)
        TextView mItemTopicUserName;
        @BindView(R.id.item_topic_replay_count)
        TextView mItemTopicReplayCount;
        @BindView(R.id.item_topic_see_count)
        TextView mItemTopicSeeCount;
        @BindView(R.id.item_topic_all)
        CardView mItemTopicAll;
        @BindView(R.id.item_topic_user_icon)
        ImageView mItemTopicUserIcon;
        @BindView(R.id.item_topic_goodOrTop)
        Button mItemTopicGoodOrTop;
        @BindView(R.id.item_topic_title)
        TextView mItemTopicTitle;
        @BindView(R.id.item_topic_time)
        TextView mItemTopicTime;
        @BindView(R.id.item_topic_replay_time)
        TextView mItemTopicReplayTime;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
